package com.liangwj.springbootdesignpatterns.structureTypePatterns.filter;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * <b>Description:</b> test <br/>
 * @author Lingwenjun
 * @date 2019/1/24-17:08
 * @version 1.0
 */
public class FilterPatternDemo {

    public static void main(String[] args) {
        List<Person> persons = new ArrayList<>();

        persons.add(new Person("a1", "male", "single"));
        persons.add(new Person("b1", "female", "married"));
        persons.add(new Person("c1", "male", "single"));
        persons.add(new Person("d1", "female", "married"));
        persons.add(new Person("e1", "male", "married"));
        persons.add(new Person("f1", "female", "single"));

        Criteria male = new CriteriaMale();
        Criteria female = new CriteriaFemale();
        Criteria single = new CriteriaSingle();
        Criteria singleMale = new AndCriteria(male, single);
        Criteria singleFemale = new AndCriteria(female, single);
        Criteria singleOrFemale = new OrCriteria(single, female);

        System.out.println("males:");
        male.meetCriteria(persons).forEach(System.out::println);

        System.out.println("females:");
        female.meetCriteria(persons).forEach(System.out::println);

        System.out.println("singleAndMales:");
        singleMale.meetCriteria(persons).forEach(System.out::println);

        System.out.println("singleAndFemales:");
        singleFemale.meetCriteria(persons).forEach(System.out::println);

        System.out.println("singleOrFemales:");
        singleOrFemale.meetCriteria(persons).forEach(System.out::println);

        System.out.println("=======================================");
        //类似jdk1.8的 分组筛选
        persons.stream().filter(person -> person.getGender().equalsIgnoreCase("male")).forEach(System.out::println);
        //多个条件判断
        System.out.println("=================mutil predicate======================");
        persons.stream().filter(person -> person.getGender().equalsIgnoreCase("male")
                && person.getStatus().equalsIgnoreCase("single")).forEach(System.out::println);

        //分组筛选
        System.out.println("==================groupingBy=====================");
        persons.stream().collect(Collectors.groupingBy(Person::getGender))//按照性别分组
                .forEach((k, v) -> {
                    System.out.println("k = " + k);
                    v.forEach(System.out::println);
                });

    }
}
